﻿package UIComponents// ActionScript file
{
	import Classes.GameDatas.GD_BuildingItem;
	
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;

	public class PanelBuild extends Sprite
	{
		public static const SHOWDECORATIONSSHOP:String="showDecorationsShop";
		public static const SHOWBUILDINGSSHOP:String="showBuildingsShop";
		public static const SHOWBUILDINGSSTORAGE:String="showBuildingsStorage";
		
		private var _showState:String=SHOWBUILDINGSSTORAGE;
		private var _showIndex:int=0;
		private var _shopDecorationsAry:Array;/*商店装饰物数组*/
		private var _shopBuildingsAry:Array;/*商店建筑数组*/
		private var _storageBuildingsAry:Array;/*仓库建筑数组*/
		public var exchangeItemNum:TextField;
		public var button_building:MyButton;
		public function PanelBuild():void
		{
			this.init();
		}
		
		private function init():void
		{
			(this.getChildByName("button_storage") as MyButton).lockState=true;
			this.exchangeItemNum=(this.getChildByName("specialItemSp") as DisplayObjectContainer).getChildByName("txt_num") as TextField;
			this.button_building=this.getChildByName("button_building") as MyButton;
			this.addListener();
		}
		
		private function addListener():void
		{
			(this.getChildByName("button_decoration") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,showDecorationsShopHandler);
			(this.getChildByName("button_building") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,showBuildingsShopHandler);
			(this.getChildByName("button_storage") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,showBuildingsStorageHandler);
			(this.getChildByName("button_turnLeft") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftHandler);
			(this.getChildByName("button_turnLeftLeft") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftLeftHandler);
			(this.getChildByName("button_turnRight") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightHandler);
			(this.getChildByName("button_turnRightRight") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightRightHandler);
		}
		
		
		private function showDecorationsShopHandler(e:ComponentEvent):void
		{
			this.dispatchEvent(new PanelEvent(PanelEvent.ADDARROWTOZS,true));
			if(this.showState==SHOWDECORATIONSSHOP)return;
			this._showState=SHOWDECORATIONSSHOP;
			this._showIndex=0;
			this.reshow();
			this.setButtonVisible();
			setBtnState(true,false,false);
		}
		
		private function setBtnState(d:Boolean,b:Boolean,s:Boolean):void
		{
			(this.getChildByName("button_decoration") as MyButton).lockState=d;
			(this.getChildByName("button_building") as MyButton).lockState=b;
			(this.getChildByName("button_storage") as MyButton).lockState=s;
		}
		
		private function showBuildingsShopHandler(e:ComponentEvent):void
		{
			this.dispatchEvent(new PanelEvent(PanelEvent.ADDARROWTOBUILID,true));
			if(this.showState==SHOWBUILDINGSSHOP)return;
			this._showState=SHOWBUILDINGSSHOP;
			this._showIndex=0;
			this.reshow();
			this.setButtonVisible();
			setBtnState(false,true,false);
		}
		
		private function showBuildingsStorageHandler(e:ComponentEvent):void
		{
			this.dispatchEvent(new PanelEvent(PanelEvent.ADDARROWTOCK,true));
			if(this.showState==SHOWBUILDINGSSTORAGE)return;
			this._showState=SHOWBUILDINGSSTORAGE;
			this._showIndex=0;
			this.reshow();
			this.setButtonVisible();
			setBtnState(false,false,true);
		}
		
		/**
		 *设置左，右翻页按扭是否可见 
		 * @param showLeft	boolean		true为左可见
		 * @param showRight boolean		true为右可见
		 * 
		 */
		private function setButtonVisible():void
		{
			var filter:ColorMatrixFilter = new ColorMatrixFilter([0.3,0.6,0,0,0,0.3,0.6,0,0,0,0.3,0.6,0,0,0,0,0,0,1,0]) ;
			
			var showLeft:Boolean		=		this._showIndex>0;
			var showRight:Boolean		=		this.nowShowingAry.length>6		&&	this.showIndex < this.nowShowingAry.length-6;
			
			if(showLeft){
				(this.getChildByName("button_turnLeft") as MyButton).mouseChildren			=		true;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseChildren		=		true;
				(this.getChildByName("button_turnLeft") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseEnabled		=		true;				
				(this.getChildByName("button_turnLeft") as MyButton).filters					=		null;
				(this.getChildByName("button_turnLeftLeft") as MyButton).filters				=		null;
			}
			else
			{
				(this.getChildByName("button_turnLeft") as MyButton).mouseChildren			=		false;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseChildren		=		false;
				(this.getChildByName("button_turnLeft") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("button_turnLeftLeft") as MyButton).mouseEnabled		=		false;					
				(this.getChildByName("button_turnLeft") as MyButton).filters					=		[filter];
				(this.getChildByName("button_turnLeftLeft") as MyButton).filters				=		[filter];
			}
			
			if(showRight){
				(this.getChildByName("button_turnRight") as MyButton).mouseChildren			=		true;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseChildren	=		true;
				(this.getChildByName("button_turnRight") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseEnabled		=		true;				
				(this.getChildByName("button_turnRight") as MyButton).filters				=		null;
				(this.getChildByName("button_turnRightRight") as MyButton).filters			=		null;
			}
			else
			{
				(this.getChildByName("button_turnRight") as MyButton).mouseChildren			=		false;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseChildren	=		false;
				(this.getChildByName("button_turnRight") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("button_turnRightRight") as MyButton).mouseEnabled		=		false;					
				(this.getChildByName("button_turnRight") as MyButton).filters				=		[filter];
				(this.getChildByName("button_turnRightRight") as MyButton).filters			=		[filter];
			}
		}
		
		private function turnLeftHandler(e:ComponentEvent):void
		{
			if(this.showIndex==0)return;
			this._showIndex-=1;
			this.reshow();
		}
		
		private function turnLeftLeftHandler(e:ComponentEvent):void
		{
			if(this.showIndex<6)
			{
				this._showIndex=0;
			}
			else
			{
				this._showIndex-=6;
			}
			this.reshow();
		}
		
		private function turnRightHandler(e:ComponentEvent):void
		{
			
			if(this.showIndex==this.nowShowingAry.length-6	 	||	this.nowShowingAry.length<=6)return;
			this._showIndex+=1;
			this.reshow();

		}
		
		private function turnRightRightHandler(e:ComponentEvent):void
		{
			if(this.nowShowingAry.length<=6)return;
			
			if(this.showIndex>this.nowShowingAry.length-12)
			{
				this._showIndex=this.nowShowingAry.length-6;
			}
			else
			{
				this._showIndex+=6;
			}
			this.reshow();
		}
		
		public function getFrameBuildingItemByIndex(index:int):FrameBuildingItem
		{
			if(index<0 || index>5)throw new Error("索引出错");
			return (this.getChildByName("frameBuildingItem_"+(index+1))) as FrameBuildingItem;
		}
		
		public function reshow():void
		{
			var nullSign:Boolean=false;
			for(var i:int=0;i<6;i++)
			{
				var frameBuildingItem:FrameBuildingItem=this.getFrameBuildingItemByIndex(i);
				frameBuildingItem._bmb.visible=true;
				if(nullSign)
				{
					frameBuildingItem.setBuildingItem(null);
					continue;
				}
				if(this.nowShowingAry[this.showIndex+i] is IUseItem)
				{
					var showBuilding:IUseItem=this.nowShowingAry[this.showIndex+i] as IUseItem;
					if(!showBuilding)nullSign=true;
					frameBuildingItem.setBuildingItem(showBuilding);
					
				}else{
					var ishop:IGD_ShopItem=this.nowShowingAry[this.showIndex+i] as IGD_ShopItem;
					if(!ishop)nullSign=true;
					frameBuildingItem.setBuildingItemLinkShop(ishop);
				}
					
				
			}
			
			this.setButtonVisible();
			this.dispatchEvent(new Event("buildReShow",true) );
		}
		
		public function setShopDecorationsAry(shopDecorationsAry:Array):void
		{
			this._shopDecorationsAry=shopDecorationsAry;
			if(this.showState==SHOWDECORATIONSSHOP)this.reshow();
		}
		
		public function setShopBuildingsAry(shopBuildingsAry:Array):void
		{
			this._shopBuildingsAry=shopBuildingsAry;
			if(this.showState==SHOWBUILDINGSSHOP)this.reshow();
		}
		
		public function setStorageBuildingsAry(storageBuildingsAry:Array):void
		{
			this._storageBuildingsAry=storageBuildingsAry;
			if(this.showState==SHOWBUILDINGSSTORAGE)this.reshow();
		}

		/*getter & setter*/
		public function get showState():String
		{
			return _showState;
		}

		public function get showIndex():int
		{
			return _showIndex;
		}

		public function get shopDecorationsAry():Array
		{
			return _shopDecorationsAry;
		}

		public function get shopBuildingsAry():Array
		{
			return _shopBuildingsAry;
		}

		public function get storageBuildingsAry():Array
		{
			return _storageBuildingsAry;
		}
		
		public function get nowShowingAry():Array
		{
			switch(this.showState)
			{
				case SHOWDECORATIONSSHOP:
					return this.shopDecorationsAry;
				case SHOWBUILDINGSSHOP:
					return this.shopBuildingsAry;
				case SHOWBUILDINGSSTORAGE:
					return this.storageBuildingsAry;
			}
			return null;
		}
		
		
		/**
		 *设置显示已建造 
		 * @param idAry		已经拥用的建筑id
		 * 
		 */
		public function setHaveOne(idAry:Array):void
		{
			var i:int;
			var j:uint;
			var frameBuildingItem:FrameBuildingItem
			for(i=0; i<6; i++)
			{
				
				frameBuildingItem=this.getFrameBuildingItemByIndex(i);
				frameBuildingItem.setHaveOne(false);
			}
			
			
			if(this.showState!=SHOWBUILDINGSSHOP)return ;


			
			var nullSign:Boolean=false;
			for(i=0; i<6; i++)
			{
				
				frameBuildingItem=this.getFrameBuildingItemByIndex(i);
				
				if(nullSign)
				{
					frameBuildingItem.setBuildingItem(null);
					continue;
				}
				if(this.nowShowingAry[this.showIndex+i] is IUseItem)
				{
					var showBuilding:IUseItem=this.nowShowingAry[this.showIndex+i] as IUseItem;
					if(!showBuilding)nullSign=true;
					
					
					for(j=0; j<idAry.length; j++)
					{
						if( Number(idAry[j])==showBuilding.itemId){
							frameBuildingItem.setHaveOne(true);
							break;
						}
					}
					
				}else{
					var ishop:IGD_ShopItem=this.nowShowingAry[this.showIndex+i] as IGD_ShopItem;
					if(!ishop){
						nullSign=true;
						continue;
					}
					
					frameBuildingItem.setHaveOne(false);
					for(j=0; j<idAry.length; j++)
					{
						if( Number(idAry[j])==ishop.shopItemId){
							frameBuildingItem.setHaveOne(true);
							break;
						}
					}
					
					if(j>=idAry.length)
					for(var k:uint=0; k<this.storageBuildingsAry.length; k++)
					{
							var ishopIt:IUseItem	=	this.storageBuildingsAry[k] as IUseItem;
							if(ishopIt!=null){
					
								var id:Number  	=	Number( ishopIt.itemId.toString().substring(0,7) + "1");
//								trace(ishop.shopItemId, ishop.shopItemType,"--",ishopIt.itemId,ishopIt.itemName,id);
								if(ishop.shopItemId	 == id)	
								{
									frameBuildingItem.setHaveOne(true);
									break;
								}
							}
						
					}
				}				
				
			}
			
			
		}


	}
}